package com.yuke.cloud.service.pmc.service.impl;

import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.opc.dto.UploadFile;
import com.yuke.cloud.service.api.opc.service.OpcFileFeignApi;
import com.yuke.cloud.service.pmc.dao.ProdSpecPictureMapper;
import com.yuke.cloud.service.pmc.entity.ProdSpecPicture;
import com.yuke.cloud.service.pmc.service.ProdSpecPictureService;
import com.yuke.cloud.common.core.support.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by zys on 2018/09/27.
 */
@Slf4j
@Service
@Transactional
public class ProdSpecPictureServiceImpl extends BaseService<ProdSpecPicture> implements ProdSpecPictureService {

    @Resource
    private ProdSpecPictureMapper prodSpecPictureMapper;
    @Resource
    private OpcFileFeignApi opcFileFeignApi;

    @Override
    public int save(ProdSpecPicture record) {
        record.setSpecPictureId(generateId());
        return super.save(record);
    }

    @Override
    public int update(ProdSpecPicture entity) {
        int count = super.update(entity);
        if (count > 0) {
            this.deleteOldPicFromFastDFS(entity);
        }
        return count;
    }

    private void deleteOldPicFromFastDFS(ProdSpecPicture prodSpecPicture) {
        UploadFile uploadFile = new UploadFile();
        List<String> filePaths = new ArrayList<>();
        if (prodSpecPicture.getOldSmallPicUri() != null && !"".equals(prodSpecPicture.getOldSmallPicUri()))
            filePaths.add(prodSpecPicture.getOldSmallPicUri());
        if (prodSpecPicture.getOldBigPicUri() != null && !"".equals(prodSpecPicture.getOldBigPicUri()))
            filePaths.add(prodSpecPicture.getOldBigPicUri());
        if (filePaths.size() > 0) {
            uploadFile.setFilePaths(filePaths);
            opcFileFeignApi.filesDelete(uploadFile);
        }
    }

    @Override
    public int delete(ProdSpecPicture record) {
        // mod by wg 20190515 修改成根据ID删除
        ProdSpecPicture temp = new ProdSpecPicture();
        temp.setSpecPictureId(record.getSpecPictureId());
        int count = super.delete(temp);
//        int count = super.delete(record);
        if (count > 0) {
            this.deleteOldPicFromFastDFS(record);
        }
        return count;
    }

    @Override
    public List<ProdSpecPicture> getProdSpecPicBySpecIds(List<Long> specIds) {
        return prodSpecPictureMapper.getProdSpecPicBySpecIds(specIds);
    }

    @Override
    public int deleteBySpecIds(List<Long> specIds) {
        return prodSpecPictureMapper.deleteBySpecIds(specIds);
    }


    public void deleteFromFastDFS(List<ProdSpecPicture> prodSpecPictures) {
        List<UploadFile> uploadFiles = new ArrayList<>();
        for (ProdSpecPicture picture : prodSpecPictures) {
            UploadFile uploadFileSmall = new UploadFile();
            uploadFileSmall.setFilePath(picture.getSmallPicUri());
            uploadFiles.add(uploadFileSmall);
            UploadFile uploadFileBig = new UploadFile();
            uploadFileBig.setFilePath(picture.getBigPicUri());
            uploadFiles.add(uploadFileBig);
        }
        Wrapper<Integer> deleteWrapper = opcFileFeignApi.fileDelete(uploadFiles);
        if (deleteWrapper.error()) {
            log.error("opcFileFeignApi.fileDelete(uploadFiles)内部异常，参数：{}", uploadFiles);
        }
        log.info("picture status is :{}", deleteWrapper.getResult() );
    }
}
